package com.bigdata.service.me;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.SearchResultMapper;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @ClassNameEsService
 * @Description
 * @Author lly
 * @Date2020/3/27 21:17
 * @Version V1.0
 **/
@Service
public class EsService {
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    public void createDoc() {
        elasticsearchTemplate.createIndex("", "");

    }
//
//    @Override
//    public List<ArticleInfo> search(Integer page, Integer pageSize, String searchContent) {
//        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
//                .should(QueryBuilders.matchQuery("name", searchContent))
//                .should(QueryBuilders.matchQuery("content", searchContent));
//
//        NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
//                .withQuery(boolQueryBuilder)
//                .withHighlightFields(new HighlightBuilder.Field("content"), new HighlightBuilder.Field("name"))
//                .withHighlightBuilder(new HighlightBuilder().preTags("<span style='color:red'>").postTags("</span>")).build();
//        AggregatedPage<ArticleInfo> articleInfos = elasticsearchTemplate.queryForPage(nativeSearchQuery, ArticleInfo.class, new SearchResultMapper() {
//            @Override
//            public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
//                ArrayList<ArticleInfo> articleInfos = new ArrayList<ArticleInfo>();
//                SearchHits hits = response.getHits();
//                for (SearchHit searchHit : hits) {
//                    if (hits.getHits().length <= 0) {
//                        return null;
//                    }
//                    Map<String, Object> sourceAsMap = searchHit.getSourceAsMap();
//                    String name = (String) sourceAsMap.get("name");
//                    String content = (String) sourceAsMap.get("content");
//                    String id = (String) sourceAsMap.get("id");
//                    System.out.println(name);
//                    System.out.println(content);
//                    ArticleInfo articleInfo = new ArticleInfo();
//                    HighlightField contentHighlightField = searchHit.getHighlightFields().get("content");
//                    if (contentHighlightField == null) {
//                        articleInfo.setContent(content);
//                    } else {
//                        String highLightMessage = searchHit.getHighlightFields().get("content").fragments()[0].toString();
//                        articleInfo.setContent(StringUtil.stripHtml(highLightMessage).replaceAll("_", ""));
//                    }
//                    HighlightField nameHighlightField = searchHit.getHighlightFields().get("name");
//                    if (nameHighlightField == null) {
//                        articleInfo.setName(name);
//                    } else {
//                        articleInfo.setName(searchHit.getHighlightFields().get("name").fragments()[0].toString());
//                    }
//                    articleInfo.setId(Long.valueOf(id));
//
//                    articleInfos.add(articleInfo);
//                }
//                if (articleInfos.size() > 0) {
//                    return new AggregatedPageImpl<T>((List<T>) articleInfos);
//                }
//                return null;
//
//            }
//        });
//        return articleInfos.getContent();
//    }
}
